package com.cloud.controller.api.file;

import com.cloud.cache.user.CloudUserUtils;
import com.cloud.domain.file.Folder;
import com.cloud.controller.from.file.ResourceFrom;
import com.cloud.param.file.ResourceParam;
import com.cloud.service.file.FolderService;
import com.common.utils.tools.result.BaseReturn;
import com.common.utils.tools.result.ServiceResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
@RequestMapping("/api/resource")
public class ResourceController {

    @Autowired
    FolderService folderService;

    @RequestMapping("/upload")
    @CrossOrigin(origins = {"http://localhost:9527","http://101.200.148.185","http://101.200.148.185:80","http://coisini.cloud"})
    @ResponseBody
    //Long parentId, @RequestParam("files")MultipartFile[] files
    public BaseReturn upload(ResourceFrom resourceFrom) throws Exception {
        ResourceParam resourceParam = new ResourceParam();
//        resourceParam.setFileId(parentId);
//        resourceParam.setFiles(files);
        BeanUtils.copyProperties(resourceFrom,resourceParam);
        resourceParam.setUserId(CloudUserUtils.getUserId());
        resourceParam.setUserName(CloudUserUtils.getUserName());
        ServiceResult serviceResult = folderService.upload(resourceParam);
        if(serviceResult.getSuccess()){
            return BaseReturn.build("200", serviceResult.getReturnMsg());
        }else {
            return BaseReturn.build("500", serviceResult.getReturnMsg());
        }

    }

    @RequestMapping("/download")
    public Boolean download(Long id, HttpServletResponse response) throws Exception {
        Folder folder = folderService.getById(id);
        String path = folder.getPath();
        FileInputStream ips = null;
        ServletOutputStream out = null;
        String fileName = folder.getFileName();
        String url = path+"/"+fileName;
        File file = new File(url);
        String finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器
        try {
            ips = new FileInputStream(file);
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            out = response.getOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = ips.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
            ips.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;




    }


}
